<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            ref="searchInput"
            placeholder="请输入内容"
            clearable
            @clear="getGoodsList"
            v-model="queryData.query"
            @click="getGoodsList"
          >
            <el-button
              slot="append"
              @click="search"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addJump">添加商品</el-button>
        </el-col>
        <!-- 商品表格 -->
        <el-table :data="goodsList" style="width: 100%" border stripe>
          <el-table-column label="#" type="index"></el-table-column>
          <el-table-column
            prop="goods_name"
            label="商品名称"
            width="650"
          ></el-table-column>
          <el-table-column
            prop="goods_price"
            label="价格"
            width="68"
          ></el-table-column>
          <el-table-column
            prop="goods_number"
            label="数量"
            width="68"
          ></el-table-column>
          <el-table-column prop="add_time" label="添加时间">
            <template slot-scope="scope">
              {{ scope.row.add_time | dateFormat }}
            </template>
          </el-table-column>
          <!-- 操作列 -->
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="doDel(scope.row)"
              ></el-button
            ></template>
          </el-table-column>
        </el-table>
        <!-- 页码条 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="pageChange"
          :current-page="queryData.pagenum"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="queryData.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  created () {
    this.getGoodsList()
  },
  data () {
    return {
      // 商品列表查询条件
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品列表总数
      goodsList: [],
      //   总商品数量
      total: 0
    }
  },
  methods: {
    // 商品列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    //
    handleSizeChange (newSize) {
      this.queryData.pagesize = newSize
      //   重新渲染表格数据
      this.getGoodsList()
    },
    //
    pageChange (newPage) {
      this.queryData.pagenum = newPage
      this.getGoodsList()
    },
    // 搜索按钮时间
    search () {
      if (this.queryData.query.trim().length === 0) {
        // 让文本框获得焦点
        this.$refs.searchInput.focus()
        //
        return this.$message.warning('请输入搜索关键字~ ：）')
      }
      // 请求商品列表
      this.getGoodsList()
    },
    // 删除事件
    doDel (rowData) {
      this.$messageBox.confirm('确定要删除吗？', '系统提示', {
        confirmButtonText: '确定',
        callback: async (action) => {
          if (action === 'cancel') return
          // 请求接口
          const { data: res } = await this.$http.delete(
            `goods/${rowData.goods_id}`
          )
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.$message.success(res.meta.msg)
          this.getGoodsList()
        }
      })
    },
    // 跳转到 添加商品页面
    addJump () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
